package com.burgeon.backend.SHOP.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Consultant;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * @author: 伯俊软件
 * @description: 查询是否绑定专属导购
 * @date: 2021/6/25 15:07
 * @param
 * @return
 */
public class FindConsultant extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindConsultant(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public Consultant getData(String phone) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(phone),url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)){
            throw new Exception(message);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        if ("null".equals(jsonArray.getJSONArray(0).getString(0))){
            return null;
        }
        Consultant consultant = new Consultant();
        consultant.setId(Integer.parseInt(jsonArray.getJSONArray(0).getString(0)));
        consultant.setName(jsonArray.getJSONArray(0).getString(1));
        consultant.setPhone(jsonArray.getJSONArray(0).getString(2));
        consultant.setStore(jsonArray.getJSONArray(0).getString(3));
        consultant.setSex(jsonArray.getJSONArray(0).getString(4));
        return consultant;
    }

    private MultiValueMap Data(String phone){
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'HR_EMPLOYEE_ID1;ID'");
        columns.add("'HR_EMPLOYEE_ID1;NAME'");
        columns.add("'HR_EMPLOYEE_ID1;PHONE'");
        columns.add("'HR_EMPLOYEE_ID1;C_STORE_ID;NAME'");
        columns.add("'HR_EMPLOYEE_ID1;SEX'");


        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        HashMap paramss = new HashMap();

        paramss.put("column","CARDNO");
        paramss.put("condition","'"+phone+"'");

        params.put("params",paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindConsultant g = new FindConsultant("39.104.240.134:55321","nea@burgeon.com.cn","root55321");
        try {
            System.err.println(g.getData("15632176185"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
